﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Configuration;
using System.Collections.Specialized;

namespace Royalsoft.Common
{
    public class FileManage
    {
        /// <summary>
        /// 保存网上办公系统文件
        /// </summary>
        /// <param name="ContractFile">上传的文件</param>
        /// <param name="SavePath">保存的路径</param>
        /// <param name="SavePath2">保存的路径</param>
        /// <returns></returns>
        public static bool SaveFileOnlineSystemFiles(HttpFileCollection ContractFile, out string SavePath)
        {
            //获得上传的文件保存文件夹
            string strSaveFolder = ConfigurationManager.AppSettings["OnlineSystemSaveFolder"];
            bool bDoValue = false;
            string strSavePath = "";
            string strSaveLocation = "";
            //相对路径
            DateTime dtime = System.DateTime.Now;
            strSavePath = strSaveFolder + dtime.ToString("yyyy") + "/" + dtime.ToString("MM") + "/" + dtime.ToString("dd") + "/";
            //物理路径
            strSaveLocation = HttpContext.Current.Server.MapPath(strSavePath);
            SavePath = "";

            try
            {
                //判断路径是否存在，如果不存在创建路径//
                if (!System.IO.Directory.Exists(strSaveLocation))
                {
                    System.IO.Directory.CreateDirectory(strSaveLocation);
                }
                //真是的文件保存路径，包括文件名的，要保存到数据库里的路径

                if (ContractFile[0].ContentLength > 0)
                {
                    SavePath = strSavePath + Guid.NewGuid().ToString() + ContractFile[0].FileName.Substring(ContractFile[0].FileName.LastIndexOf("."));
                    ContractFile[0].SaveAs(HttpContext.Current.Server.MapPath(SavePath));
                }


                bDoValue = true;
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
            finally
            {
            }
            return bDoValue;
        }

        /// <summary>
        /// 保存网上办公系统图片---重载,用于多个上传控件FileUpLoad
        /// </summary>
        /// <param name="ContractFile">上传的文件</param>
        /// <param name="SavePath">保存的路径</param>
        /// <param name="SavePath2">保存的路径</param>
        /// <returns></returns>
        public static bool SaveFileOnlineSystemFiles(HttpFileCollection ContractFile, out string SavePath, out string SavePath2, out string SavePath3)
        {
            //获得上传的文件保存文件夹
            string strSaveFolder = ConfigurationManager.AppSettings["OnlineSystemSaveFolder"];
            bool bDoValue = false;
            string strSavePath = "";
            string strSaveLocation = "";
            //相对路径
            DateTime dtime = System.DateTime.Now;
            strSavePath = strSaveFolder + dtime.ToString("yyyy") + "/" + dtime.ToString("MM") + "/" + dtime.ToString("dd") + "/";
            //物理路径
            strSaveLocation = HttpContext.Current.Server.MapPath(strSavePath);
            SavePath = "";
            SavePath2 = "";
            SavePath3 = "";

            try
            {
                //判断路径是否存在，如果不存在创建路径//
                if (!System.IO.Directory.Exists(strSaveLocation))
                {
                    System.IO.Directory.CreateDirectory(strSaveLocation);
                }
                //真是的文件保存路径，包括文件名的，要保存到数据库里的路径

                if (ContractFile[0].ContentLength > 0)
                {

                    SavePath = strSavePath + Guid.NewGuid().ToString() + ContractFile[0].FileName.Substring(ContractFile[0].FileName.LastIndexOf("."));
                    ContractFile[0].SaveAs(HttpContext.Current.Server.MapPath(SavePath));
                }
                if (ContractFile[1].ContentLength > 0)
                {
                    SavePath2 = strSavePath + Guid.NewGuid().ToString() + ContractFile[1].FileName.Substring(ContractFile[1].FileName.LastIndexOf("."));
                    ContractFile[1].SaveAs(HttpContext.Current.Server.MapPath(SavePath2));
                }

                if (ContractFile[2].ContentLength > 0)
                {
                    SavePath3 = strSavePath + Guid.NewGuid().ToString() + ContractFile[2].FileName.Substring(ContractFile[2].FileName.LastIndexOf("."));
                    ContractFile[2].SaveAs(HttpContext.Current.Server.MapPath(SavePath3));
                }

                bDoValue = true;
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
            finally
            {
            }
            return bDoValue;
        }

        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="ContractFile">上传的文件</param>
        /// <param name="SavePath">保存的路径</param>
        /// <param name="SavePath2">保存的路径</param>
        /// <returns></returns>
        public static bool SaveFile(HttpFileCollection ContractFile, string SaveLocalPath, string FileName, string FileType)
        {
            bool bDoValue = false;
            try
            {
                //判断路径是否存在，如果不存在创建路径//
                if (!System.IO.Directory.Exists(SaveLocalPath))
                {
                    System.IO.Directory.CreateDirectory(SaveLocalPath);
                }
                //真是的文件保存路径，包括文件名的，要保存到数据库里的路径

                if (ContractFile[0].ContentLength > 0)
                {
                    SaveLocalPath = SaveLocalPath + FileName + FileType;
                    ContractFile[0].SaveAs(SaveLocalPath);
                }

                bDoValue = true;
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
            finally
            {
            }
            return bDoValue;
        }
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="ContractFile">上传的文件</param>
        /// <param name="SavePath">保存的路径</param>
        /// <param name="SavePath2">保存的路径</param>
        /// <returns></returns>
        public static bool SaveFile(HttpFileCollection ContractFile, string SaveLocalPath, string FileName, string FileType,int fileUploadIndex)
        {
            bool bDoValue = false;
            try
            {
                //判断路径是否存在，如果不存在创建路径//
                if (!System.IO.Directory.Exists(SaveLocalPath))
                {
                    System.IO.Directory.CreateDirectory(SaveLocalPath);
                }
                //真是的文件保存路径，包括文件名的，要保存到数据库里的路径

                if (ContractFile[fileUploadIndex].ContentLength > 0)
                {
                    SaveLocalPath = SaveLocalPath + FileName + FileType;
                    ContractFile[fileUploadIndex].SaveAs(SaveLocalPath);
                }
                bDoValue = true;
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
            finally
            {
            }
            return bDoValue;
        }
        /// <summary>
        /// 根据路径删除指定的文件
        /// </summary>
        /// <param name="FilePath">图片相对路径</param>
        /// <returns></returns>
        public static bool DeletePicture(string FilePath)
        {
            bool bReturnValue = false;
            string strFilePath = HttpContext.Current.Server.MapPath(FilePath);
            try
            {
                if (System.IO.File.Exists(strFilePath))
                {
                    System.IO.File.Delete(strFilePath);
                }

                bReturnValue = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
            return bReturnValue;
        }
    }

}
